package com.itheima.dao;

import com.itheima.controller.DeptController;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 用于数据的获取
 */

public class DeptDaoImpl {
    private static final Logger logger = LoggerFactory.getLogger(DeptController.class);
    public List<String> list() throws FileNotFoundException {

        URL url = this.getClass().getResource("/dept.txt");
        if (url != null) {
            logger.info("dept.txt file path: {}", url.getPath());
        } else {
            logger.error("dept.txt file not found in classpath");
            throw new FileNotFoundException("dept.txt file not found in classpath");
        }

        InputStream input = this.getClass().getResourceAsStream("/dept.txt");//一定要加斜杠，才能编译成功
        if (input == null) {
            logger.error("dept.txt file not found in classpath");
            throw new FileNotFoundException("dept.txt file not found in classpath");
        }

        List<String> strings = IOUtils.readLines(input, StandardCharsets.UTF_8);
        return strings;
    }

}
